(function($) {

	$.global = {

		init: function() {
			
			var navMenu = $(".ps_nav"),
				shortInfo = $(".ps_nav_de"),
				leaveMessageButton = $('#leave_message'),
				anonymousLogin = $('#anonymous-login');

			$(shortInfo).hover(function(e) {
				$(shortInfo).hide();
				navMenu.show();
			});
			
			$(navMenu).bind('mouseenter',function(){
				$(navMenu).stop(true, true);
			}).bind("mouseleave", function(){
				$(navMenu).delay(200).fadeOut(300);
				$(shortInfo).show();
			});
			
			leaveMessageButton.wokaobox({
				'width'			: 500,
				'height'		: 360,
				'type'			: 'iframe',
				'openMethod'	: 'changeUp',
				'closeMethod'	: 'changeOut'
			});
			
			anonymousLogin.wokaobox({
				'width'			: 582,
				'height'		: 290,
				'type'			: 'iframe',
				'openMethod'	: 'changeIn',
				'closeMethod'	: 'changeOut',
				'afterClose'	: function(){
					if($('#weibo_auth').attr('href') != 'javascript:;'){
						
						$.wokaobox.open({
							'href'			: $('#weibo_auth').attr('href'),
							'width'			: 570,
							'height'		: 349,
							'type'			: 'iframe',
							'openMethod'	: 'changeIn',
							'closeMethod'	: 'changeOut',
							'afterClose'	: function(){
								//$('#weibo_auth').attr('href', 'javascript:;');
								location.reload();
							}
						});
					}
				}
			});
		},
		
		getNewMessages: function(){

			var newFresh = $('#newFresh', $('#header')),
				newNotice = $('#newNotice', $('#header'));
				
				interval = setInterval(function(){
					getMessages();
				},60000),
			
				getMessages = function(){
				
					$.ajax({
						url: $.global.getContextPath() + '/push/message/count',
						type:'GET',
						dataType:'json',
						beforeSend: function (xhr) {
					        xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
					   	},
					  	statusCode: {
							403: function() {
								 window.clearInterval(interval);
							}
						},
						success:function(json){
							if(json.notice > 0){
								newNotice.text(json.notice).parents('.tip_count_wrap').show();
								if(json.notice > 9){
									newNotice.addClass('gt9');
								}
							}
							else{
								 newNotice.text(json.notice).parents('.tip_count_wrap').hide();
							}
							if(json.feed > 0){
								newFresh.text(json.feed).parents('.tip_count_wrap').show();
								if(json.feed > 9){
									newFresh.addClass('gt9');
								}
							}
							else{
								newFresh.text(json.feed).parents('.tip_count_wrap').hide();
							}
						}
					});
					return false;
				};
				
			getMessages();
		},

		getContextPath: function() {
			var contextPath = $('#context_path', $('#global_paths'));
			return contextPath.val();
		},

		getImagePath: function() {
			var imagesPath = $('#images_path', $('#global_paths'));
			return imagesPath.val();
		},

		getAvatarPath: function() {
			var avatarPath = $('#avatar_path', $('#global_paths'));
			return avatarPath.val();
		},

		getCourseImgPath: function() {
			var courseImgPath = $('#course_img_path', $('#global_paths'));
			return courseImgPath.val();
		}
	};
})(jQuery);
